import "package:flutter/material.dart";
import "package:myapp/_views/dynamic/type.dart";
import "../../../_common/geyTextPainter.dart";

class DynamicArticleWidget extends StatelessWidget {
  final DynamicArticleItem dynamicArticleItem;
  const DynamicArticleWidget({
    super.key,
    required this.dynamicArticleItem,
  });
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding:const EdgeInsets.all(8),
      child: LayoutBuilder(builder: (
        BuildContext context,
        BoxConstraints constraints,
      ) {
        double maxWidth = constraints.maxWidth;
        return Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Text(
              dynamicArticleItem.title,
              style: const TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 17,
                height: 1.6,
              ),
            ),
            DynamicArticleContentWidget(
              articleContent: dynamicArticleItem.content,
              maxWidth: maxWidth,
            ),
            dynamicArticleItem.coverUrl != null
                ? Padding(
                    padding: const EdgeInsets.only(bottom: 8),
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(4),
                      child: Image(
                        image:
                            AssetImage(dynamicArticleItem.coverUrl as String),
                        fit: BoxFit.cover,
                        width: double.infinity,
                      ),
                    ),
                  )
                : const SizedBox()
          ],
        );
      }),
    );
  }
}

class DynamicArticleContentWidget extends StatelessWidget {
  final String articleContent;
  final int maxLines;
  final double maxWidth;
  const DynamicArticleContentWidget({
    super.key,
    required this.articleContent,
    this.maxLines = 4,
    required this.maxWidth,
  });
  @override
  Widget build(BuildContext context) {
    String showStr = ''; //待显示内容
    TextStyle textStyle = const TextStyle(
      fontSize: 17,
      height: 1.6,
      color: Colors.black,
    );
    final textPainer = getTextPainter(
      context,
      text: articleContent,
      maxLines: maxLines,
      style: textStyle,
      maxWidth: maxWidth,
    );
    final moreText = textPainer.didExceedMaxLines; //是否超出限制行
    if (moreText) {
      final textSize = textPainer.size; //容器的宽高信息
      final position = textPainer
          .getPositionForOffset(Offset(double.infinity, textSize.height));
      final endOffset = position.offset - 3; //为按钮预留空间
      showStr = articleContent.substring(0, endOffset);
    }
    RichText txt = RichText(
      text: TextSpan(
        children: [
          TextSpan(
            text: showStr,
            style: textStyle,
          ),
          TextSpan(
            text: '...',
            style: textStyle,
          ),
          const TextSpan(
            text: '全文',
            style: TextStyle(
              fontSize: 17,
              height: 1.6,
              color: Colors.blue,
            ),
          ),
        ],
      ),
    );
    return Padding(
      padding: const EdgeInsets.only(bottom: 8),
      child: moreText
          ? txt
          : Text(
              articleContent,
              style: textStyle,
            ),
    );
  }
}
